import {signin} from "../../lib/auth";
import {getUser, setUser, setUserPlaylist} from "../../store/user";


Page({
    data: {
        email: '',
        password: '',
        error: '', // 报错信息
        disable: false // 防抖, 禁止短时间重复提交
    },
    // 清空表单
    clearForm() {
        this.setData({
            email: '',
            password: '',
        })
    },
    setDisable(disable) {
        this.setData({disable})
    },
    setError(error) {
        this.setData({error})
    },
    inputEmail(e) {
        this.setData({
            email: e.detail.value
        })
    },
    inputPassword(e) {
        this.setData({
            password: e.detail.value
        })
    },
    submit(e) {
        this.setDisable(true)
        console.log(this.data.email, this.data.password)
        signin(this.data.email, this.data.password,
            (res) => {
                if (res.statusCode === 500) {
                    this.setError('账号或密码错误')
                    this.setDisable(false)
                    return
                }
                // console.log(res)
                // 记录登录信息
                setUser(res.data.user)
                setUserPlaylist(res.data.user.musicList)
                // console.log(getUser())
                // 清空输入框
                this.clearForm();
                this.setDisable(false)
                this.setError(undefined)
                // 跳转index
                // navigateTo:fail can not navigateTo a tabbar page
                // wx.navigateTo({url: '../index/index'})
                // wx.reLaunch({url: '../index/index'})
                wx.switchTab({url: '../index/index'})
            },
        )
    },
    toSignup(e) {
        wx.reLaunch({
            url: '../signup/signup',
        })
    },
    onLoad(options) {

    },
    onReady() {

    },
    onShow() {

    },
    onHide() {

    },
    onUnload() {

    },
    onShareAppMessage() {
        return {
            title: '',
        };
    },
});